home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / add-log.el < prev    next >
Lisp/Scheme  |  1994-07-23  |  17KB  |  459 lines

  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: maint
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; This facility is documented in the Emacs Manual.
  26.  
  27. ;;; Code:
  28.  
  29. (defvar change-log-default-name nil
  30.   "*Name of a change log file for \\[add-change-log-entry].")
  31.  
  32. (defvar add-log-current-defun-function nil
  33.   "\
  34. *If non-nil, function to guess name of current function from surrounding text.
  35. \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
  36. instead) with no arguments.  It returns a string or nil if it cannot guess.")
  37.  
  38. ;; This MUST not be autoloaded, since user-login-name
  39. ;; cannot be known at Emacs dump time.
  40. (defvar add-log-full-name (user-full-name)
  41.   "*Full name of user, for inclusion in ChangeLog daily headers.
  42. This defaults to the value returned by the `user-full-name' function.")
  43.  
  44. ;; This MUST not be autoloaded, since user-login-name
  45. ;; cannot be known at Emacs dump time.
  46. (defvar add-log-mailing-address user-mail-address
  47.   "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
  48. This defaults to the value of `user-mail-address'.")
  49.  
  50. (defun change-log-name ()
  51.   (or change-log-default-name
  52.       (if (eq system-type 'vax-vms) 
  53.       "$CHANGE_LOG$.TXT" 
  54.     (if (eq system-type 'ms-dos)
  55.         "changelo"
  56.       "ChangeLog"))))
  57.  
  58. ;;;###autoload
  59. (defun prompt-for-change-log-name ()
  60.   "Prompt for a change log name."
  61.   (let ((default (change-log-name)))
  62.     (expand-file-name
  63.      (read-file-name (format "Log file (default %s): " default)
  64.              nil default))))
  65.  
  66. ;;;###autoload
  67. (defun find-change-log (&optional file-name)
  68.   "Find a change log file for \\[add-change-log-entry] and return the name.
  69.  
  70. Optional arg FILE-NAME specifies the file to use.
  71. If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
  72. Otherwise, search in the current directory and its successive parents
  73. for a file named `ChangeLog' (or whatever we use on this operating system).
  74.  
  75. Once a file is found, `change-log-default-name' is set locally in the
  76. current buffer to the complete file name."
  77.   ;; If user specified a file name or if this buffer knows which one to use,
  78.   ;; just use that.
  79.   (or file-name
  80.       (setq file-name change-log-default-name)
  81.       (progn
  82.     ;; Chase links in the source file
  83.     ;; and use the change log in the dir where it points.
  84.     (setq file-name (or (and buffer-file-name
  85.                  (file-name-directory
  86.                   (file-chase-links buffer-file-name)))
  87.                 default-directory))
  88.     (if (file-directory-p file-name)
  89.         (setq file-name (expand-file-name (change-log-name) file-name)))
  90.     ;; Chase links before visiting the file.
  91.     ;; This makes it easier to use a single change log file
  92.     ;; for several related directories.
  93.     (setq file-name (file-chase-links file-name))
  94.     (setq file-name (expand-file-name file-name))
  95.     ;; Move up in the dir hierarchy till we find a change log file.
  96.     (let ((file1 file-name)
  97.           parent-dir)
  98.       (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
  99.               (progn (setq parent-dir
  100.                    (file-name-directory
  101.                     (directory-file-name
  102.                      (file-name-directory file1))))
  103.                  ;; Give up if we are already at the root dir.
  104.                  (not (string= (file-name-directory file1)
  105.                        parent-dir))))
  106.         ;; Move up to the parent dir and try again.
  107.         (setq file1 (expand-file-name 
  108.              (file-name-nondirectory (change-log-name))
  109.              parent-dir)))
  110.       ;; If we found a change log in a parent, use that.
  111.       (if (or (get-file-buffer file1) (file-exists-p file1))
  112.           (setq file-name file1)))))
  113.   ;; Make a local variable in this buffer so we needn't search again.
  114.   (set (make-local-variable 'change-log-default-name) file-name)
  115.   file-name)
  116.  
  117. ;;;###autoload
  118. (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
  119.   "Find change log file and add an entry for today.
  120. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  121. Second arg is file name of change log.  If nil, uses `change-log-default-name'.
  122. Third arg OTHER-WINDOW non-nil means visit in other window.
  123. Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
  124. never append to an existing entry."
  125.   (interactive (list current-prefix-arg
  126.              (prompt-for-change-log-name)))
  127.   (if whoami
  128.       (progn
  129.     (setq add-log-full-name (read-input "Full name: " add-log-full-name))
  130.      ;; Note that some sites have room and phone number fields in
  131.      ;; full name which look silly when inserted.  Rather than do
  132.      ;; anything about that here, let user give prefix argument so that
  133.      ;; s/he can edit the full name field in prompter if s/he wants.
  134.     (setq add-log-mailing-address
  135.           (read-input "Mailing address: " add-log-mailing-address))))
  136.   (let ((defun (funcall (or add-log-current-defun-function
  137.                 'add-log-current-defun)))
  138.     paragraph-end entry)
  139.  
  140.     (setq file-name (expand-file-name (find-change-log file-name)))
  141.  
  142.     ;; Set ENTRY to the file name to use in the new entry.
  143.     (and buffer-file-name
  144.      ;; Never want to add a change log entry for the ChangeLog file itself.
  145.      (not (string= buffer-file-name file-name))
  146.      (setq entry (if (string-match
  147.               (concat "^" (regexp-quote (file-name-directory
  148.                              file-name)))
  149.               buffer-file-name)
  150.              (substring buffer-file-name (match-end 0))
  151.                (file-name-nondirectory buffer-file-name))))
  152.  
  153.     (if (and other-window (not (equal file-name buffer-file-name)))
  154.     (find-file-other-window file-name)
  155.       (find-file file-name))
  156.     (or (eq major-mode 'change-log-mode)
  157.     (change-log-mode))
  158.     (undo-boundary)
  159.     (goto-char (point-min))
  160.     (if (looking-at (concat (regexp-quote (substring (current-time-string)
  161.                              0 10))
  162.                 ".* " (regexp-quote add-log-full-name)
  163.                 "  <" (regexp-quote add-log-mailing-address)))
  164.     (forward-line 1)
  165.       (insert (current-time-string)
  166.           "  " add-log-full-name
  167.           "  <" add-log-mailing-address ">\n\n"))
  168.  
  169.     ;; Search only within the first paragraph.
  170.     (if (looking-at "\n*[^\n* \t]")
  171.     (skip-chars-forward "\n")
  172.       (forward-paragraph 1))
  173.     (setq paragraph-end (point))
  174.     (goto-char (point-min))
  175.  
  176.     ;; Now insert the new line for this entry.
  177.     (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
  178.        ;; Put this file name into the existing empty entry.
  179.        (if entry
  180.            (insert entry)))
  181.       ((and (not new-entry)
  182.         (re-search-forward
  183.          (concat (regexp-quote (concat "* " entry))
  184.              ;; Don't accept `foo.bar' when
  185.              ;; looking for `foo':
  186.              "\\(\\s \\|[(),:]\\)")
  187.          paragraph-end t))
  188.        ;; Add to the existing entry for the same file.
  189.        (re-search-forward "^\\s *$\\|^\\s \\*")
  190.        (beginning-of-line)
  191.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  192.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  193.        (insert "\n\n")
  194.        (forward-line -2)
  195.        (indent-relative-maybe))
  196.       (t
  197.        ;; Make a new entry.
  198.        (forward-line 1)
  199.        (while (looking-at "\\sW")
  200.          (forward-line 1))
  201.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  202.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  203.        (insert "\n\n\n")
  204.        (forward-line -2)
  205.        (indent-to left-margin)
  206.        (insert "* " (or entry ""))))
  207.     ;; Now insert the function name, if we have one.
  208.     ;; Point is at the entry for this file,
  209.     ;; either at the end of the line or at the first blank line.
  210.     (if defun
  211.     (progn
  212.       ;; Make it easy to get rid of the function name.
  213.       (undo-boundary)
  214.       (insert (if (save-excursion
  215.             (beginning-of-line 1)
  216.             (looking-at "\\s *$")) 
  217.               ""
  218.             " ")
  219.           "(" defun "): "))
  220.       ;; No function name, so put in a colon unless we have just a star.
  221.       (if (not (save-excursion
  222.          (beginning-of-line 1)
  223.          (looking-at "\\s *\\(\\*\\s *\\)?$")))
  224.       (insert ": ")))))
  225.  
  226. ;;;###autoload
  227. (defun add-change-log-entry-other-window (&optional whoami file-name)
  228.   "Find change log file in other window and add an entry for today.
  229. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  230. Second arg is file name of change log.  \
  231. If nil, uses `change-log-default-name'."
  232.   (interactive (if current-prefix-arg
  233.            (list current-prefix-arg
  234.              (prompt-for-change-log-name))))
  235.   (add-change-log-entry whoami file-name t))
  236. ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
  237.  
  238. ;;;###autoload
  239. (defun change-log-mode ()
  240.   "Major mode for editing change logs; like Indented Text Mode.
  241. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  242. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  243. Each entry behaves as a paragraph, and the entries for one day as a page.
  244. Runs `change-log-mode-hook'."
  245.   (interactive)
  246.   (kill-all-local-variables)
  247.   (indented-text-mode)
  248.   (setq major-mode 'change-log-mode
  249.     mode-name "Change Log"
  250.     left-margin 8
  251.     fill-column 74)
  252.   (use-local-map change-log-mode-map)
  253.   ;; Let each entry behave as one paragraph:
  254.   (set (make-local-variable 'paragraph-start) "^\\s *$\\|^\f")
  255.   (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^\f\\|^\\sw")
  256.   ;; Let all entries for one day behave as one page.
  257.   ;; Match null string on the date-line so that the date-line
  258.   ;; is grouped with what follows.
  259.   (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
  260.   (set (make-local-variable 'version-control) 'never)
  261.   (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  262.   (run-hooks 'change-log-mode-hook))
  263.  
  264. (defvar change-log-mode-map nil
  265.   "Keymap for Change Log major mode.")
  266. (if change-log-mode-map
  267.     nil
  268.   (setq change-log-mode-map (make-sparse-keymap))
  269.   (define-key change-log-mode-map "\M-q" 'change-log-fill-paragraph))
  270.  
  271. ;; It might be nice to have a general feature to replace this.  The idea I
  272. ;; have is a variable giving a regexp matching text which should not be
  273. ;; moved from bol by filling.  change-log-mode would set this to "^\\s *\\s(".
  274. ;; But I don't feel up to implementing that today.
  275. (defun change-log-fill-paragraph (&optional justify)
  276.   "Fill the paragraph, but preserve open parentheses at beginning of lines.
  277. Prefix arg means justify as well."
  278.   (interactive "P")
  279.   (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
  280.     (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
  281.     (fill-paragraph justify)))
  282.  
  283. (defvar add-log-current-defun-header-regexp
  284.   "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
  285.   "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
  286.  
  287. ;;;###autoload
  288. (defun add-log-current-defun ()
  289.   "Return name of function definition point is in, or nil.
  290.  
  291. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  292. Texinfo (@node titles), and Fortran.
  293.  
  294. Other modes are handled by a heuristic that looks in the 10K before
  295. point for uppercase headings starting in the first column or
  296. identifiers followed by `:' or `=', see variable
  297. `add-log-current-defun-header-regexp'.
  298.  
  299. Has a preference of looking backwards."
  300.   (condition-case nil
  301.       (save-excursion
  302.     (let ((location (point)))
  303.       (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
  304.          ;; If we are now precisely a the beginning of a defun,
  305.          ;; make sure beginning-of-defun finds that one
  306.          ;; rather than the previous one.
  307.          (or (eobp) (forward-char 1))
  308.          (beginning-of-defun)
  309.          ;; Make sure we are really inside the defun found, not after it.
  310.          (if (and (progn (end-of-defun)
  311.                  (< location (point)))
  312.               (progn (forward-sexp -1)
  313.                  (>= location (point))))
  314.              (progn
  315.                (if (looking-at "\\s(")
  316.                (forward-char 1))
  317.                (forward-sexp 1)
  318.                (skip-chars-forward " ")
  319.                (buffer-substring (point)
  320.                      (progn (forward-sexp 1) (point))))))
  321.         ((and (memq major-mode '(c-mode c++-mode c++-c-mode))
  322.               (save-excursion (beginning-of-line)
  323.                       ;; Use eq instead of = here to avoid
  324.                       ;; error when at bob and char-after
  325.                       ;; returns nil.
  326.                       (while (eq (char-after (- (point) 2)) ?\\)
  327.                     (forward-line -1))
  328.                       (looking-at "[ \t]*#[ \t]*define[ \t]")))
  329.          ;; Handle a C macro definition.
  330.          (beginning-of-line)
  331.          (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
  332.            (forward-line -1))
  333.          (search-forward "define")
  334.          (skip-chars-forward " \t")
  335.          (buffer-substring (point)
  336.                    (progn (forward-sexp 1) (point))))
  337.         ((memq major-mode '(c-mode c++-mode c++-c-mode))
  338.          (beginning-of-line)
  339.          ;; See if we are in the beginning part of a function,
  340.          ;; before the open brace.  If so, advance forward.
  341.          (while (not (looking-at "{\\|\\(\\s *$\\)"))
  342.            (forward-line 1))
  343.          (or (eobp)
  344.              (forward-char 1))
  345.          (beginning-of-defun)
  346.          (if (progn (end-of-defun)
  347.                 (< location (point)))
  348.              (progn
  349.                (backward-sexp 1)
  350.                (let (beg tem)
  351.  
  352.              (forward-line -1)
  353.              ;; Skip back over typedefs of arglist.
  354.              (while (and (not (bobp))
  355.                      (looking-at "[ \t\n]"))
  356.                (forward-line -1))
  357.              ;; See if this is using the DEFUN macro used in Emacs,
  358.              ;; or the DEFUN macro used by the C library.
  359.              (if (condition-case nil
  360.                  (and (save-excursion
  361.                     (end-of-line)
  362.                     (while (= (preceding-char) ?\\)
  363.                       (end-of-line 2))
  364.                     (backward-sexp 1)
  365.                     (beginning-of-line)
  366.                     (setq tem (point))
  367.                     (looking-at "DEFUN\\b"))
  368.                       (>= location tem))
  369.                    (error nil))
  370.                  (progn
  371.                    (goto-char tem)
  372.                    (down-list 1)
  373.                    (if (= (char-after (point)) ?\")
  374.                    (progn
  375.                      (forward-sexp 1)
  376.                      (skip-chars-forward " ,")))
  377.                    (buffer-substring (point)
  378.                          (progn (forward-sexp 1) (point))))
  379.                ;; Ordinary C function syntax.
  380.                (setq beg (point))
  381.                (if (condition-case nil
  382.                    ;; Protect against "Unbalanced parens" error.
  383.                    (progn
  384.                      (down-list 1) ; into arglist
  385.                      (backward-up-list 1)
  386.                      (skip-chars-backward " \t")
  387.                      t)
  388.                  (error nil))
  389.                    ;; Verify initial pos was after
  390.                    ;; real start of function.
  391.                    (if (and (save-excursion
  392.                       (goto-char beg)
  393.                       ;; For this purpose, include the line
  394.                       ;; that has the decl keywords.  This
  395.                       ;; may also include some of the
  396.                       ;; comments before the function.
  397.                       (while (and (not (bobp))
  398.                               (save-excursion
  399.                             (forward-line -1)
  400.                             (looking-at "[^\n\f]")))
  401.                         (forward-line -1))
  402.                       (>= location (point)))
  403.                     ;; Consistency check: going down and up
  404.                     ;; shouldn't take us back before BEG.
  405.                     (> (point) beg))
  406.                    (buffer-substring (point)
  407.                              (progn (backward-sexp 1)
  408.                                 (point))))))))))
  409.         ((memq major-mode
  410.                '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
  411.                   plain-tex-mode latex-mode;; cmutex.el
  412.                   ))
  413.          (if (re-search-backward
  414.               "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
  415.              (progn
  416.                (goto-char (match-beginning 0))
  417.                (buffer-substring (1+ (point));; without initial backslash
  418.                      (progn
  419.                        (end-of-line)
  420.                        (point))))))
  421.         ((eq major-mode 'texinfo-mode)
  422.          (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
  423.              (buffer-substring (match-beginning 1)
  424.                        (match-end 1))))
  425.                 ((eq major-mode 'fortran-mode)
  426.                  ;; must be inside function body for this to work
  427.                  (beginning-of-fortran-subprogram)
  428.                  (let ((case-fold-search t)) ; case-insensitive
  429.                    ;; search for fortran subprogram start
  430.                    (if (re-search-forward
  431.              "^[ \t]*\\(program\\|subroutine\\|function\
  432. \\|[ \ta-z0-9*]*[ \t]+function\\)"
  433.              nil t)
  434.                        (progn
  435.                          ;; move to EOL or before first left paren
  436.                          (if (re-search-forward "[(\n]" nil t)
  437.                  (progn (forward-char -1)
  438.                     (skip-chars-backward " \t"))
  439.                (end-of-line))
  440.              ;; Use the name preceding that.
  441.                          (buffer-substring (point)
  442.                                            (progn (forward-sexp -1)
  443.                                                   (point)))))))
  444.         (t
  445.          ;; If all else fails, try heuristics
  446.          (let (case-fold-search)
  447.            (end-of-line)
  448.            (if (re-search-backward add-log-current-defun-header-regexp
  449.                        (- (point) 10000)
  450.                        t)
  451.                (buffer-substring (match-beginning 1)
  452.                      (match-end 1))))))))
  453.     (error nil)))
  454.  
  455.  
  456. (provide 'add-log)
  457.  
  458. ;;; add-log.el ends here
  459.